<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
 * Date: 20/10/2008
 * File: functions.members.php
 */

function getMembers($limit, $perpage, $search) {
    global $smarty;
    $members = array();
		$c = "SELECT * FROM ".TABLE_USERS." WHERE id > 0";
    if (strlen($search) > 0) {
        $c .= " AND (username LIKE '%".checkSQL($search)."%' OR fullname LIKE '%".checkSQL($search)."%')";
    }
    // order results
    $c .= " ORDER BY id DESC";
    if (strlen($perpage) > 0) {
        // get total results
        $q2 = mysql_query(str_replace("*","count(*) as numrows",$c)) or die(mysql_error());
        $r2 = mysql_fetch_assoc($q2);
        if ($r2) {
            $numrows = $r2['numrows'];
        }
        $maxPage = ceil($numrows/$perpage);
        // by default we show first page
        $pageNum = 1;
        // if $_GET['page'] defined, use it as page number
        if(isset($_GET['page'])) { $pageNum = $_GET['page']; }
        // counting the offset
        $offset = ($pageNum - 1) * $perpage;
        $c .= " LIMIT $offset, $perpage";
    } else {
        // Instead of using pagination show all results in one page and limit them to limit(IF SET)
        if (strlen($limit) > 0) {
            $c .= " LIMIT 0, $limit";
        }
    }
    $q = mysql_query($c) or die(mysql_error());
    while ($r = mysql_fetch_assoc($q)) {
				$photo = Hop_getUserPhoto($r['photo']);
        $tmp = array('id' => $r['id'], 'username' => $r['username'], 'fullname' => nl2br($r['fullname']), 'lastLogin' => parseDate($r['last_login']), 'photo' => $photo);
		$members[] = $tmp;
        unset($tmp);
    }
    if (strlen($perpage) > 0) {
        // pagination
        $nav  = "";
        for($page = 1; $page <= $maxPage; $page++)
        {
           if ($page == $pageNum) {
              $nav .= " $page "; // no need to create a link to current page
           } else {
              $nav .= " <a href=\"".urlWithout("page")."page=$page\">$page</a> ";
           }
        }
        if ($pageNum > 1) {
           $page  = $pageNum - 1;
           $prev  = " <a href=\"".urlWithout("page")."page=$page\">".l('Atrás')."</a> ";
           $first = " <a href=\"".urlWithout("page")."page=1\">".l('Primera Página')."</a> ";
        } else {
           $prev  = '&nbsp;'; // we're on page one, don't print previous link
           $first = '&nbsp;'; // nor the first page link
        }

        if ($pageNum < $maxPage) {
           $page = $pageNum + 1;
           $next = " <a href=\"".urlWithout("page")."page=$page\">".l('Adelante')."</a> ";

           $last = " <a href=\"".urlWithout("page")."page=$maxPage\">".l('Última Página')."</a> ";
        } else {
           $next = '&nbsp;'; // we're on the last page, don't print next link
           $last = '&nbsp;'; // nor the last page link
        }
        $smarty->assign('paging_nav', $nav);
        $smarty->assign('paging_first', $first);
        $smarty->assign('paging_prev', $prev);
        $smarty->assign('paging_next', $next);
        $smarty->assign('paging_last', $last);
    }
    return $members;
}

?>
